/**
*    Copyright (C) 2008  Chase Kernan 
*    chase.kernan@gmail.com
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
* 
**/

package com.chasekernan.hxnova.core.components;

import com.chasekernan.hxnova.core.components.Component;
import com.chasekernan.hxnova.core.components.ComponentSet;
import haxe.xml.Check;

/**
    Includes smart bombs and bombs that only damage factories.
**/
class Bomb extends Component {
    
    public var minimumKill(default, setMinKill) : Int;
    private function setMinKill(mk : Int) : Int {if(mk < 0) {throw "min kill must be >= 0.";} else {minimumKill = mk;} return minimumKill;}
    
    public var percentKill(default, setPKill) : Float;
    private function setPKill(pk : Float) : Float {if(pk < 0) {throw "percentKill must be >= 0";} else {percentKill = pk;} return percentKill;}
    
    public var installationsDestroyed(default, setIDestroyed) : Int;
    private function setIDestroyed(id : Int) : Int {if(id < 0) {throw "installationsDestroyed must be >= 0";} else {installationsDestroyed = id;} return installationsDestroyed;}
    
    public function new(name : String, ?minimumKill : Int, ?percentKill : Float,
            ?installationsDestroyed : Int) {
        super(name);
        setMinKill(if(minimumKill == null) 0 else minimumKill);
        setPKill(if(percentKill == null) 0.01 else percentKill);
        setIDestroyed(if(installationsDestroyed == null) 0 else 
                installationsDestroyed);
    }
    
    public function isSmartBomb() : Bool {
        return installationsDestroyed == 0 && minimumKill == 0;
    }
    
    public static function writeToXml(b : Bomb) : Xml {
        return Component.writeToXml(b);
    }
    
    public static function readFromXml(xml : Xml) : Bomb {
        try {
            var b = new Bomb("");
            //Check.checkNode(xml, b.getRules());
            b.generateFromComponentXml(xml, b);
            
            return b;
        } catch (e : Dynamic) {
            throw 'Bad xml file for bomb: '+e;
        }
    }
}